﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;

namespace QuanLyThuNgan
{
    public partial class frm_DM_LoaiDichVuYTe : DevExpress.XtraEditors.XtraForm
    {
        public frm_DM_LoaiDichVuYTe()
        {
            InitializeComponent();
        }

        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        DMLoaiDichVuYTeDTO loaiDVYTDto = new DMLoaiDichVuYTeDTO();
        DMLoaiDVYTCTL loaiDVYTCtl = new DMLoaiDVYTCTL();

        private void frm_DM_LoaiDichVuYTe_Load(object sender, EventArgs e)
        {
            DataTable dtload = loaiDVYTCtl.m_SelectAllDMLoaiDVYT();
            gridControlLoaiDVYTe.DataSource = dtload;

            //set condition load form----------------------------
            gvLoaiDVuYTe.OptionsBehavior.ReadOnly = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
            gvLoaiDVuYTe.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gridControlLoaiDVYTe.DataSource;
            if (dt != null)
            {
                for (int i = 0; i < gvLoaiDVuYTe.RowCount; i++)
                {
                    DataRow dr = gvLoaiDVuYTe.GetDataRow(i);
                    gvLoaiDVuYTe.MoveNext();
                    gvLoaiDVuYTe.MovePrev();
                    if (dr != null)
                    {
                        loaiDVYTDto.STenLoaiDichVuYTe = dr["TenLoaiDichVuYTe"].ToString().Trim();
                        loaiDVYTDto.STenVni = Uit.it_Font.UnicodeToVNI(dr["TenLoaiDichVuYTe"].ToString().Trim());

                        loaiDVYTDto.BbNgungSuDung = Uit.it_Parse.ToBoolean(dr["bNgungSuDung"].ToString());
                        if (dr["IDLoaiDichVuYTe"].ToString() == "")
                        {
                            loaiDVYTCtl.m_InsertDMLoaiDVYT(loaiDVYTDto);
                        }
                        else
                        {
                            loaiDVYTDto.IIDLoaiDichVuYTe = Uit.it_Parse.ToInteger(dr["IDLoaiDichVuYTe"].ToString());
                            loaiDVYTCtl.m_UpdateDMLoaiDVYT(loaiDVYTDto);
                        }
                    }
                }
                
            }
            
            XtraMessageBox.Show("Lưu thành công");
            DataTable dtdvytload = loaiDVYTCtl.m_SelectAllDMLoaiDVYT();
            gridControlLoaiDVYTe.DataSource = dtdvytload;

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            gvLoaiDVuYTe.OptionsBehavior.ReadOnly = true;
            gvLoaiDVuYTe.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gvLoaiDVuYTe.GetDataRow(gvLoaiDVuYTe.FocusedRowHandle);
            if (dr != null)
            {
                loaiDVYTDto.IIDLoaiDichVuYTe = Uit.it_Parse.ToInteger(dr["IDLoaiDichVuYTe"].ToString());
                loaiDVYTCtl.m_DeleteDMLoaiDVYT(loaiDVYTDto);
                             
            }
            XtraMessageBox.Show("Xóa thành công");
            DataTable dtDVYTLoad = loaiDVYTCtl.m_SelectAllDMLoaiDVYT();
            gridControlLoaiDVYTe.DataSource = dtDVYTLoad;
            //reset load du lieu----------------------
            gvLoaiDVuYTe.ActiveFilterEnabled = false;
            gvLoaiDVuYTe.ClearColumnsFilter();
            gvLoaiDVuYTe.OptionsFilter.Reset();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            gvLoaiDVuYTe.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = false;
            btnInsert.Enabled = false;
            gvLoaiDVuYTe.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //reset load du lieu----------------------
            gvLoaiDVuYTe.ActiveFilterEnabled = false;
            gvLoaiDVuYTe.ClearColumnsFilter();
            gvLoaiDVuYTe.OptionsFilter.Reset();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            gvLoaiDVuYTe.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            gvLoaiDVuYTe.OptionsBehavior.ReadOnly = true;
            DataTable dtloadDVYT = loaiDVYTCtl.m_SelectAllDMLoaiDVYT();
            gridControlLoaiDVYTe.DataSource = dtloadDVYT;
            gvLoaiDVuYTe.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gvLoaiDVuYTe.ActiveFilterEnabled = false;
            gvLoaiDVuYTe.ClearColumnsFilter();
            gvLoaiDVuYTe.OptionsFilter.Reset();
        }

        private void gvLoaiDVuYTe_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = gvLoaiDVuYTe;
            GridColumn colDVYT = view.Columns["TenLoaiDichVuYTe"];
            int iRowIndex = e.RowHandle;
            //if (sColRow != "bNgungSuDung")
            //{
            string sDVYT = view.GetRowCellValue(e.RowHandle, colDVYT).ToString().ToUpper().Trim();
            if (sDVYT == "")
            {
                e.Valid = false;
                view.SetColumnError(colDVYT, "Tên loại dịch vụ y tế không được trống");
            }

            //set vong lap kiem tra gia tri nhap vao----------
            for (int i = 0; i < gvLoaiDVuYTe.RowCount; i++)
            {
                //kiem tra neu dong dang sua khac dong dang duyet-------------
                if (i != iRowIndex)
                {
                    DataRow drdvyt = gvLoaiDVuYTe.GetDataRow(i);
                    string sTestDVYT = drdvyt["TenLoaiDichVuYTe"].ToString().ToUpper().Trim();
                    if (sDVYT.ToString() == sTestDVYT)
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Loại dịch vụ y tế không được trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(colDVYT, "Loại dịch vụ y tế không được trùng");
                        return;
                    }
                    else
                    {
                        view.SetColumnError(colDVYT, "");
                    }
                }
            }      
                 
            //}
            btnSave.Enabled = true;
        }


        private void gvLoaiDVuYTe_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }
        //tim ten can search---------------------------------------------
        private void gvLoaiDVuYTe_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }
        //set ten can tim----------------------------------------------------------------
        private void gvLoaiDVuYTe_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvLoaiDVuYTe;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }


    }
}